package com.yd.sdk.payimp;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.widget.FrameLayout;

import com.facebook.ads.*;
import com.unity3d.player.MyUnityPlayerActivity;
import com.yd.sdk.ad.AdAbstractClass;

public class FacebookAdImp extends AdAbstractClass {

    private AdView _adView;
    private InterstitialAd _interstitialAd;
    private RewardedVideoAd _rewardedVideoAd;

    private boolean _isLoadingInterstitial = false;
    private boolean _isLoadingRewarded = false;
    private boolean _isShowBanner = false;

    private AdListener _bannerListener;
    private InterstitialAdListener _interstitialAdListener;
    private RewardedVideoAdListener _rewardedVideoAdListener;
    @Override
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        this.activity = activity;

        AudienceNetworkAds.initialize(activity);
        AdSettings.addTestDevice("13402846-8f2c-48b5-9695-b25a5317d774");

        InitBanner();
        InitIntersitial();
        InitReward();
    }


    private void InitBanner()
    {
        _adView = new AdView(activity, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
        _bannerListener = new AdListener() {
            @Override
            public void onError(Ad ad, AdError adError) {
                OnAdFailedToLoad(AdType.Banner,adError.getErrorCode()+":" + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                OnAdSucessToLoad(AdType.Banner);
            }

            @Override
            public void onAdClicked(Ad ad) {

            }

            @Override
            public void onLoggingImpression(Ad ad) {

            }
        };
    }

    private void InitIntersitial()
    {
        _interstitialAd = new InterstitialAd(activity, "YOUR_PLACEMENT_ID");
        _interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                OnAdOpened(AdType.Interstitial);
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                OnAdClosed(AdType.Interstitial,"");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                OnAdFailedToLoad(AdType.Interstitial,adError.getErrorCode()+":" + adError.getErrorMessage());
                _isLoadingInterstitial = false;
            }

            @Override
            public void onAdLoaded(Ad ad) {
                OnAdSucessToLoad(AdType.Interstitial);
                _isLoadingInterstitial = false;
            }

            @Override
            public void onAdClicked(Ad ad) {

            }

            @Override
            public void onLoggingImpression(Ad ad) {

            }
        };
    }

    private void InitReward()
    {
        _rewardedVideoAdListener = new RewardedVideoAdListener() {
            @Override
            public void onRewardedVideoCompleted() {
                OnAdClosed(AdType.Reward,"yes");
            }

            @Override
            public void onRewardedVideoClosed() {
                OnAdClosed(AdType.Reward,"no");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                _isLoadingRewarded = false;
                OnAdFailedToLoad(AdType.Reward, adError.getErrorCode() + ":" + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                _isLoadingRewarded = false;
                OnAdSucessToLoad(AdType.Reward);
            }

            @Override
            public void onAdClicked(Ad ad) {

            }

            @Override
            public void onLoggingImpression(Ad ad) {

            }
        };
    }




    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onDestroy() {
        if (_adView != null) {
            _adView.destroy();
        }

        if (_interstitialAd != null) {
            _interstitialAd.destroy();
        }

        if (_rewardedVideoAd != null) {
            _rewardedVideoAd.destroy();
            _rewardedVideoAd = null;
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantRes) {

    }

    @Override
    public void SetTestDeviceIds(String ids) {

    }

    @Override
    public void SetAdIds(String bannerId, String interstitialId, String rewardId) {

    }

    @Override
    public void LoadedAd(AdType adType) {
        switch (adType) {
            case Banner:
                _adView.loadAd(_adView.buildLoadAdConfig().withAdListener(_bannerListener).build());
                break;
            case Interstitial:
                if(_isLoadingInterstitial)return;
                _isLoadingInterstitial = true;

                _interstitialAd.loadAd(
                        _interstitialAd.buildLoadAdConfig()
                                .withAdListener(_interstitialAdListener)
                                .build());
                break;
            case Reward:
                if(_isLoadingRewarded)return;
                _isLoadingRewarded = true;

                _rewardedVideoAd.loadAd(
                        _rewardedVideoAd.buildLoadAdConfig()
                                .withAdListener(_rewardedVideoAdListener)
                                .build());
                break;
        }
    }

    @Override
    public boolean IsAdAvailable(AdType adType) {

        boolean result = true;
        switch (adType)
        {
            case Banner:
                break;
            case Interstitial:
                if(_interstitialAd == null || !_interstitialAd.isAdLoaded()) {
                    result = false;
                }
                // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
                if(_interstitialAd.isAdInvalidated()) {
                    result = false;
                }
                break;
            case Reward:
                if (_rewardedVideoAd == null || !_rewardedVideoAd.isAdLoaded()) {
                    result = false;
                }
                // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
                if (_rewardedVideoAd.isAdInvalidated()) {
                    result = false;
                }
                break;
        }

        return result;
    }

    @Override
    public void ShowBanner(BannerPos pos) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        lp.gravity = pos == BannerPos.Bottom? Gravity.BOTTOM : Gravity.TOP;
        if(_isShowBanner == false) {
            ((MyUnityPlayerActivity) activity).GetUnityPlayer().addView(_adView, lp);
            _isShowBanner = true;
        }
    }

    @Override
    public void ShowInterstitial() {
        if(_interstitialAd == null || !_interstitialAd.isAdLoaded()) {
            return;
        }
        // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
        if(_interstitialAd.isAdInvalidated()) {
            return;
        }
        // Show the ad
        _interstitialAd.show();
    }

    @Override
    public void ShowReward() {
        if (_rewardedVideoAd == null || !_rewardedVideoAd.isAdLoaded()) {
            return;
        }
        // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
        if (_rewardedVideoAd.isAdInvalidated()) {
            return;
        }
        _rewardedVideoAd.show();
    }

    @Override
    public void HideAd(AdType adType) {
        switch (adType) {

            case Banner:
                if(_isShowBanner)
                {
                    ((MyUnityPlayerActivity)activity).GetUnityPlayer().removeView(_adView);
                    _isShowBanner = false;
                }
                break;
            case Interstitial:

                break;
            case Reward:
                break;
        }
    }



}
